package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Ellipse;
import model.Point;
import model.utils.Utils;
import sun.security.ec.ECDSAOperations;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;

public class EllipseDisplayer extends ShapeDisplayerJFxImpl
{
    public static final Point DEFAULT_CENTER = new Point(100,100);
    public static final double DEFAULT_RADIUS_X = 40.0;
    public static final double DEFAULT_RADIUS_Y = 20.0;

    private Ellipse ellipse;

    EllipseDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Ellipse();
        shape.setStroke(DEFAULT_STROKE);
        shape.setFill(DEFAULT_FILL);
        shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        this.ellipse=(Ellipse)shape;
        this.ellipse.setCenterX(DEFAULT_CENTER.getX());
        this.ellipse.setCenterY(DEFAULT_CENTER.getY());
        this.ellipse.setRadiusX(DEFAULT_RADIUS_X);
        this.ellipse.setRadiusX(DEFAULT_RADIUS_Y);
    }

    @Override
    protected void resize(double multi)
    {
        double newRadiusX = this.ellipse.getRadiusX() * (1 + multi);
        this.ellipse.setRadiusX(newRadiusX);
        double newRadiusY = this.ellipse.getRadiusY() * (1 + multi);
        this.ellipse.setRadiusY(newRadiusY);
    }

    @Override
    public void setLocation(Point start, Point end)
    {
        Point newCenter = Utils.calcMidPoint(start,end);
        this.ellipse.setCenterX(newCenter.getX());
        this.ellipse.setCenterY(newCenter.getY());

        double radiusX = Math.abs(end.getX()-start.getX()) / 2;
        double radiusY = Math.abs(end.getY()-start.getY()) / 2;
        this.ellipse.setRadiusX(radiusX);
        this.ellipse.setRadiusY(radiusY);
    }

    @Override
    public ShapeDisplayer clone()
    {
        EllipseDisplayer copy = new EllipseDisplayer(this.canvas,this.controller);
        copy.ellipse.setCenterX(this.ellipse.getCenterX());
        copy.ellipse.setCenterY(this.ellipse.getCenterY());
        copy.ellipse.setRadiusX(this.ellipse.getRadiusX());
        copy.ellipse.setRadiusY(this.ellipse.getRadiusY());
        copy.ellipse.setStroke(this.ellipse.getStroke());
        copy.ellipse.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        return copy;
    }

    @Override
    protected Point getCenter(){return new Point(ellipse.getCenterX(),ellipse.getCenterY());}

    @Override
    protected void setCenter(Point newCenter)
    {
        this.ellipse.setCenterX(newCenter.getX());
        this.ellipse.setCenterY(newCenter.getY());
    }
}